import { Inject, Injectable } from '../decorators/inject';
import { ICellGroup, CellTypes, ICellEditor, ICellRender, DefaultCellGroup } from '../cell-types';
import { classTransformToObj, toJSON } from '../decorators/toJSON';
import { Cell } from './cell';
// import { ICellEditor } from '../types';
class DefaultRender {
    public cell: {type: string};
    render() {
        console.error(`${this.cell.type} is not found!`);
    }
}
class DefaultEditor implements ICellEditor {
    public initEditor(cell: Cell<any>, editorContainer: HTMLElement): void {
        
    }
    public finish(): void {
        
    }
}
@Injectable('CellTypeManage')
export class CellTypeManage {
    protected celltypes: Map<string, ICellGroup> = new Map();
    protected activeEditor: ICellEditor;
    protected defaultEditor: {new (...args: any[]):ICellEditor};
    constructor() {

    }
    public init() {
        
        for(let group of CellTypes) {
            this.register(group);
        }
        this.defaultEditor = this.getEditor('default')
    }
    public register(cellType: ICellGroup) {
        this.celltypes.set(cellType.type, cellType);
    }
    public getRender(type: string) {
        try {
            let cell = this.celltypes.get(type);
            return cell.render || DefaultRender;
        } catch (error) {
            if(__ENV__ === 'development') {
                throw new Error(`cell type ${type} is not found!`);
            }
            return DefaultRender;
        }
        
    }
    public getEditor(type: string) {
        let cell = this.celltypes.get(type) ;
        return cell ? cell.editor : this.defaultEditor;
    }
    public remove(type: string) {
        this.celltypes.delete(type);
    }
    public has(type: string) {
        return this.celltypes.has(type);
    }
    
    protected factory(source: any): any {
        throw new Error('factroy实现异常');
    }
}